package com.njlime.cms.repository.app;

import com.njlime.cms.entities.Community;
import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import com.njlime.core.utils.StringUtil;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Dean on 2017/10/10.
 */
@Repository
public class AppCommunityDaoImpl extends BaseDaoImpl<Community> {

    /**
     * 获取详情
     *
     * @param id
     * @return
     */
    public Map<String, Object> get(int id, Integer unit_id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" SELECT ti.* ,u.head_url,u.name,tu.unit_name FROM t_community ti ");
        sb.append(" left join t_user u on ti.unit_id = u.unit_id ");
        sb.append(" LEFT JOIN t_unit tu ON ti.unit_id = tu.id ");
        sb.append(" WHERE ti.id = ?");
        arrList.add(id);
        if (unit_id != null && unit_id.intValue() != 0) {
            sb.append(" and unit_id = ? ");
            arrList.add(unit_id);
        }
        List<Map<String, Object>> re = getListJdbc(sb.toString(), arrList);
        if (re != null && re.size() > 0) {
            return re.get(0);
        }
        return null;
    }

    /**
     * 获取列表
     *
     * @param pageBean
     * @return
     */
    public PageBean list(PageBean pageBean, Community info) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" SELECT tin.*,tu.head_url,tu.unit_name FROM t_community tin ");
        sb.append(" JOIN t_unit tu ON tin.unit_id = tu.id ");
        sb.append(" where tin.is_del = 0 and tin.publish_status = 3");
        if (info.getUnit_id() != null) {
            sb.append(" and tin.unit_id = ? ");
            arrList.add(info.getUnit_id());
        }
        if (info.getChannel_id() != null) {
            sb.append(" and tin.channel_id = ? ");
            arrList.add(info.getChannel_id());
        }
        if (info.getRoot_unit_id() != null) {
            sb.append(" and tin.root_unit_id = ? ");
            arrList.add(info.getRoot_unit_id());
        }
        if (info.getIs_prize() != null) {
            sb.append(" and tin.is_prize = ? ");
            arrList.add(info.getIs_prize());
        }
        if (info.getPosition() != null) {
            sb.append(" and tin.position = ? ");
            arrList.add(info.getPosition());
        }
        if (!StringUtil.isEmpty(info.getStartTime())) {
            sb.append(" and tin.create_time > ? ");
            arrList.add(info.getStartTime());
        }
        if (!StringUtil.isEmpty(info.getEndTime())) {
            sb.append(" and tin.create_time < ? ");
            arrList.add(info.getEndTime());
        }

        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        if (num > 0) {
            sb.append(" order by tin.position asc,tin.create_time desc limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 改变阅读数
     * @return
     */
    public int updateReading(int id, Integer unit_id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" UPDATE t_community SET reading = reading + 1 WHERE id = ?");
        arrList.add(id);
        if (unit_id != null && unit_id.intValue() != 0) {
            sb.append(" and unit_id = ? ");
            arrList.add(unit_id);
        }
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 更新评论数
     * @param communityId
     * @param num
     */
    public void updateCommentMum(Integer communityId, int num) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" UPDATE t_community SET comment_num = comment_num + ? WHERE id = ?");
        arrList.add(num);
        arrList.add(communityId);
        updateJdbc(sb.toString(), arrList);
    }

    /**
     * 更新点赞数
     * @param id
     * @param num
     */
    public void fabulousNumUpdate(Integer id, int num) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" UPDATE t_community SET fabulous_num = IFNULL(fabulous_num,0) + ? WHERE id = ?");
        arrList.add(num);
        arrList.add(id);
        updateJdbc(sb.toString(), arrList);
    }
}
